package com.tdam2011.grupo02.ui;

import java.util.Date;
import java.util.LinkedList;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.domain.MensajeWeb;

public class MsgwCustomAdapter extends BaseAdapter {

	private Context context;
	private LinkedList<MensajeWeb> smsLogInfos;
	private LayoutInflater inflater;

	public MsgwCustomAdapter(Context context) {
		this.context = context;
		this.smsLogInfos = new LinkedList<MensajeWeb>();
		inflater = LayoutInflater.from(this.context);
	}

	public MsgwCustomAdapter(LinkedList<MensajeWeb> smsLogInfos, Context context) {
		this.context = context;
		this.smsLogInfos = smsLogInfos;
		inflater = LayoutInflater.from(context);
	}

	public void add(LinkedList<MensajeWeb> smsLogInfos) {
		if (smsLogInfos != null)
			this.smsLogInfos = smsLogInfos;
	}

	@Override
	public int getCount() {
		return smsLogInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return smsLogInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		Holder holder;
		final MensajeWeb info = (MensajeWeb) getItem(position);

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_mensajesweblog, null);
			holder = new Holder();
			((CheckedTextView) (convertView
					.findViewById(R.id.chktvMensajesLogEliminar)))
					.setVisibility(View.GONE);
			holder.imgContacto = (ImageView) convertView
					.findViewById(R.id.imgvMensajesWebLogContacto);
			holder.txtContacto = (TextView) convertView
					.findViewById(R.id.txtvMensajesWebLogContacto);
			holder.txtFecha = (TextView) convertView
					.findViewById(R.id.txtvMensajesWebLogFechaHora);
			holder.txtSMS = (TextView) convertView
					.findViewById(R.id.txtvMensajesWebLogMensaje);
			holder.txtAddress = (TextView) convertView
					.findViewById(R.id.txtvMensajesWebLogAddress);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		Date installDate = new Date(info.getFecha());
		String timeFormat = DateFormat.getTimeFormat(context).format(
				installDate);
		String dateFormat = DateFormat.getDateFormat(context).format(
				installDate);

		// holder.imgContacto.setBackgroundResource(R.drawable.icon);
		holder.txtContacto.setText(info.getContacto());
		holder.txtSMS.setText(info.getMensaje());
		holder.txtFecha.setText(dateFormat + " " + timeFormat);

		if (info.getContactLocation() != null)
			holder.txtAddress.setText(info.getContactLocation().getAddress());
		else
			holder.txtAddress.setText("");

		return convertView;
	}

	class Holder {
		private ImageView imgContacto;
		private TextView txtContacto;
		private TextView txtFecha;
		private TextView txtSMS;
		private TextView txtAddress;
	}
}
